package GUILayer;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import CTRLayer.LogbookCTR;
import CTRLayer.PersonCTR;
import CTRLayer.TrainedCTR;
import ModelLayer.Person;
import ModelLayer.Trained;

public class Logbook
{
	private PersonCTR pc;
	private LogbookCTR lbc;
	private TrainedCTR tc;
	private JTextField textField;
	private JTextField textField_1;
	private Person child;
	private Person trainer;
	private Trained t;
	private ArrayList<ModelLayer.Logbook> logbooks;
	private JLabel lblDate;
	private JTextArea txtNote;
	private JFrame logbook;
	private JPanel panel_5;
	
	public Logbook()
	{
		tc= new TrainedCTR();
		lbc = new LogbookCTR();
		pc = new PersonCTR();
		logbook = new JFrame();
		logbook.setTitle("ParaManagement - Logbog");
		logbook.setBounds(100, 100, 600, 600);
		
		GregorianCalendar calendar = new GregorianCalendar();
		String [] monthName = {"Januar", "Februar",
	            "Marts", "April", "Maj", "Juni", "Juli",
	            "August", "September", "Oktober", "November",
	            "December"};
		  int year = calendar.get(GregorianCalendar.YEAR);
		  int day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
		  String nameofmonth = monthName[calendar.get(GregorianCalendar.MONTH)];
		
		JPanel panel = new JPanel();
		
		JButton btnSave = new JButton("Gem");
		btnSave.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				addLogbook();
				logbook.dispose();
			}
		});
		
		JPanel panel_2 = new JPanel();
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_3.setBackground(Color.WHITE);
		
		JLabel label = new JLabel("Logbog d. ");
		
		lblDate = new JLabel(day + "/" + nameofmonth + "/" + year);
		
		txtNote = new JTextArea();
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGap(0, 564, Short.MAX_VALUE)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGap(8)
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblDate, GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE))
				.addComponent(txtNote, GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE)
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGap(0, 210, Short.MAX_VALUE)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDate, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtNote, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
		);
		panel_3.setLayout(gl_panel_3);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap(509, Short.MAX_VALUE)
					.addComponent(btnSave)
					.addGap(22))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(177)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(254, Short.MAX_VALUE))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(6)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 221, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnSave))
		);
		
		JLabel lblAktivitet = new JLabel("Aktivitet:");
		
		JLabel lblBarn = new JLabel("Barn:");
		
		textField = new JTextField("");
		textField.setBorder(null);
		textField.setEditable(false);
		textField.setColumns(10);
		
		textField_1 = new JTextField("");
		textField_1.setBorder(null);
		textField_1.setEditable(false);
		textField_1.setColumns(10);
		
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblAktivitet)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblBarn)
							.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
							.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(5)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAktivitet)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBarn)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(logbook.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 584, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 327, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
		);
		
		JLabel lblTidligereNoter = new JLabel("Tidligere Noter:");
		
		panel_5 = new JPanel();
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(10)
							.addComponent(lblTidligereNoter))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 565, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addComponent(lblTidligereNoter)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_5, GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JTextArea textArea = new JTextArea();
		textArea.setEditable(false);
		panel_5.add(textArea);
		
		
		panel_1.setLayout(gl_panel_1);
		logbook.getContentPane().setLayout(groupLayout);
		logbook.setVisible(true);
		
		getChild();
		getTrained();
		getLogbooks();
	}
	
	public void getChild()
	{
		String phonenr = "98211345";
		child = pc.findPersonPhoneNr(phonenr);
		
		if(child == null)
        {
        	textField_1.setText("Barnet eksisterer ikke.");
        }
        else
        {
        	textField_1.setText(child.getfName() + " " + child.getlName());
        }
    }
	
	public void getTrained()
	{
		int trainedID = 1;
		t = tc.findTrainedID(trainedID);
		
		if(t == null)
        {
        	textField.setText("Aktivitet eksisterer ikke.");
        }
        else
        {
        	textField.setText(t.getName());
        }
	}
	
	public void addLogbook()
	{
    	String date = lblDate.getText();
    	String note = txtNote.getText();
    	int trainedID = 1;
    	LogbookCTR.createLogbook(date, note, trainedID);
	}
	
	public void getLogbooks()
	{
		int trainedID = 1;
		ArrayList<ModelLayer.Logbook> lblist = lbc.findAllLogbooksByTrainedID(trainedID);
		if(lblist == null)
		{
			
		}
		else
		{
			for(ModelLayer.Logbook logbook : lblist)
			{
				JTextField jtf = new JTextField();
				jtf.setBorder(null);
				jtf.setEditable(false);
				jtf.setSize(200, 50);
				jtf.setText(logbook.getNote());
				panel_5.add(jtf);
			}
		}
	}
}
